<include file="./Tpl/Public/header.html" />
<h1>
    <span class="action-span1">{$current_operation[g][title]}->{$current_operation[m][title]}->{$current_operation[a][title]}</span>
    <div style="clear:both"></div>
</h1>
<div class="dialog-div">
<table width="100%" border="1" cellspacing="0" cellpadding="0">
<tr>
	  <th align="left"><strong>应用信息</strong></th></tr>
    <tr>
          <td>
          <foreach name="list_app" item="app">
          <div style="float:left; width:20%"><input type="radio" name="app_info" class="app_info" value="{$app[name]}" app_id='{$app[id]}' menu_id='{$app[menu_id]}' /><a href="javascript:alert('{$app[remark]}');" title="{$app[remark]}">{$app[name]}【{$app[title]}】</a></div>
       	</foreach>     
        <script language="javascript">
		$('.app_info').click(function(){
			var app_name=$(this).val();
			var app_id=$(this).attr('app_id');
			$('.app_name').val(app_name);
			$('.app_id').val(app_id);
			$('.module_name').val('');
			$('.module_id').val('');
			var url="{:U('Sys/Module/radio_list?app_id=')}"+app_id;
			$('#module_list').load(url);
		});
        </script>
       </td></tr>
	<tr>
	  <th align="left"><strong>默认模块信息</strong></th></tr>
	<tr><td>
       <table width="100%" border="1" cellspacing="0" cellpadding="0">
         <tr>
           <td colspan="2" valign="top">
           <div id="module_list"></div>
        <script language="javascript">
		function select_module(dom){
			var module_name=$(dom).val();
			var module_id=$(dom).attr('module_id');
			var menu_id=$(dom).attr('menu_id');
			$('.action_menu').val(menu_id);
			$('.module_name').val(module_name);
			$('.module_id').val(module_id);
		}
        </script>
           </td>
           <td valign="top" width="45%">
             <div style="height:140px; overflow:scroll">
               数据表：<input name="table_name" type="text" id="table_name" value="{$_REQUEST[table_name]}" />
               无需填写前缀,如需添加字段请到“<a href="{:U('Sys/Field/index')}" style="color:#690" target="_blank">字段管理</a>”中增加
               <script language="javascript">
//取得数据表中的数据字段
get_field();
$("#table_name").blur(function(){
	$(".table_name").val($("#table_name").val());
	get_field();
});
function get_field(){
	var table_name=$("#table_name").val();
	var uri="{:U('Sys/Field/get_field?table_name=')}"+table_name;
	$.ajax({
		type: "get",
		url: uri,
		dataType:"html",
		success: function(html){
			$("#action_file_list").html(html);
		}
	});
}
</script>
               <div id="action_file_list"></div>
               </div>
             </td>
         </tr>
	  </table>
      </td></tr>
      <tr>
	  <th align="left"><strong>生成操作</strong></th></tr>
	<tr>
	  <td>
       <form method='post'  id="form_action" action="{:U('Sys/Action/add_batch')}">
			<input name="act" type="hidden" id="act" value="todo">
            <input name="app_name" type="hidden" class="app_name" value="" />
			<input name="app_id" type="hidden" class="app_id" value="" />
            <input name="module_name" type="hidden" class="module_name" value="" />
			<input name="module_id" type="hidden" class="module_id" value="" />
            <input type="hidden" value="[1][2]" class="menu_role_ids" name="menu_role_ids">
      <table width="100%" border="1" cellspacing="0" cellpadding="0">
                 <tr>
                  <th>操作</th>
                  <th>模板</th>
                  <th>标题</th>
                  <th>名称</th>
                  <th>说明</th>
                </tr>
                <tr>
                  <td><input type="checkbox" checked="checked" value="index" name="create_data[]" class="create_data_index create_data" onclick="create_menu('.create_data_index','index')">
                  <a href="#" onclick="alert($('.create_data:checked').length);">列表</a></td>
                  <td>
                    <select name="index[tpl]" id="index_tpl" onchange="set_menu('index')">
                      <option value="index" selected="selected" url="￥url" target="main-frame" position="menu">默认列表模板</option>
                      <option url="￥url" target="main-frame" position="menu" value="index_1">列表带复选框模板</option>
                  </select>
                  <!--生成菜单要传过去的参数-->
                  <input type="hidden" value="￥url" id="index_menu_url" name="index[menu_url]">
                  <input type="hidden" value="main-frame" id="index_menu_target" name="index[menu_target]">
                  <input type="hidden" value="menu" id="index_menu_position" name="index[menu_position]">
                  </td>
                  <td><input name="index[title]" type="text" class="title" value="数据列表" id="index_title" onblur="set_action_pre();">
                    <input name="index[menu]" type="checkbox" class="action_menu index_menu" value="1" checked="checked" style="display:none"/>
                    <input name="index[menu_status]" type="checkbox" value="1" checked="CHECKED" />
                    显示菜单
                    <script language="javascript">
                    //设置操作的前缀
					function set_action_pre(){
						$("#index_title").val();
						alert('系统暂时未实现自动转换为拼音功能，请自己填写前缀，如"sjlb_list"');
						/*
						var uri='{:U("Sys?role_id=$_GET[role_id]")}';
						$.ajax({
							type: "get",
							url: uri,
							dataType:"html",
							success: function(html){
								$("#dialog_add").html(html);
								$("#dialog_add").dialog("open");
							}
						});
						*/
					}
                    </script>
                  </td>
                  <td><input name="index[name]" type="text" class="name" id="index_name" value="sjlb_list">
                  <input name="index[table_name]" type="hidden" class="table_name" value="{$_REQUEST[table_name]}" /></td>
                  <td><textarea cols="57" rows="3" name="index[remark]" id="index[remark]"></textarea></td>
                </tr>
                <tr>
                  <td><input type="checkbox" checked="checked" value="add" name="create_data[]" class="create_data_add create_data" onclick="create_menu('.create_data_add','add')">添加</td>
                  <td>
                    <select name="add[tpl]" id="add_tpl" onchange="set_menu('add')">
                      <option url="javascript:dialog_add();" target="_self" position="main" value="add" url="" target="" position="">默认添加对话框模板</option>
                      <option url="￥url" target="_blank" position="main" value="add_new">新开窗口模板</option>
                  </select>
                  <!--生成菜单要传过去的参数-->
                  <input type="hidden" value="javascript:dialog_add();" id="add_menu_url" name="add[menu_url]">
                  <input type="hidden" value="_self" id="add_menu_target" name="add[menu_target]">
                  <input type="hidden" value="main" id="add_menu_position" name="add[menu_position]">
                  </td>
                  <td><input name="add[title]" type="text" class="title" value="添加" id="add[title]">
                  	<input name="add[menu]" type="checkbox" class="action_menu add_menu" value="1" checked="checked" style="display:none"/>
                    <input name="add[menu_status]" type="checkbox" value="1" checked="checked" />
                  显示菜单</td>
                  <td><input name="add[name]" type="text" class="name" id="add_name" value="sjlb_add"><input name="add[table_name]" type="hidden" class="table_name" value="{$_REQUEST[table_name]}" /></td>
                  <td><textarea cols="57" rows="3" name="add[remark]" id="add[remark]"></textarea></td>
                </tr>
                <tr>
                  <td><input type="checkbox" checked="checked" value="edit" name="create_data[]" class="create_data_edit create_data" onclick="create_menu('.create_data_edit','edit')">修改</td>
                  <td>
                    <select name="edit[tpl]" id="edit_tpl" onchange="set_menu('edit')">
                      <option url="javascript:diaolog_edit();" target="_self" position="main" value="edit">默认修改对话框模板</option>
                      <option url="￥url" target="_blank" position="main" value="edit_new">新开修改模板</option>
                  </select>
                    <input type="hidden" value="javascript:diaolog_edit();" id="edit_menu_url" name="edit[menu_url]" />
                    <input type="hidden" value="_self" id="edit_menu_target" name="edit[menu_target]" />
                  <input type="hidden" value="main" id="edit_menu_position" name="edit[menu_position]" />
                  </td>
                  <td><input name="edit[title]" type="text" class="title" value="修改" id="edit[title]">
                  	<input name="edit[menu]" type="checkbox" class="action_menu edit_menu" value="1" checked="checked" style="display:none"/>
                    <input name="edit[menu_status]" type="checkbox" value="1" />
                    显示菜单</td>
                  <td><input name="edit[name]" type="text" class="name" id="edit_name" value="sjlb_edit"><input name="edit[table_name]" type="hidden" class="table_name" value="{$_REQUEST[table_name]}" /></td>
                  <td><textarea cols="57" rows="3" name="edit[remark]" id="edit[remark]"></textarea></td>
                </tr>
                <tr>
                  <td><input type="checkbox" checked="checked" value="delete" name="create_data[]" class="create_data_delete create_data" onclick="create_menu('.create_data_delete','delete')">
                  删除</td>
                  <td>
                    <select name="delete[tpl]" id="delete_tpl" onchange="set_menu('delete')">
                      <option>无需模板</option>
                    </select>
                    <input type="hidden" value="javascript:diaolog_delete();" id="delete_menu_url" name="delete[menu_url]" />
                    <input type="hidden" value="_self" id="delete_menu_target" name="delete[menu_target]" />
                  <input type="hidden" value="main" id="delete_menu_position" name="delete[menu_position]" />
                  </td>
                  <td><input name="delete[title]" type="text" class="title" value="禁用" id="delete[title]">
                  <input name="delete[menu]" type="checkbox" class="action_menu delete_menu" value="1" checked="checked" style="display:none"/>
                  <input name="delete[menu_status]" type="checkbox" value="1" />
                    显示菜单</td>
                  <td><input name="delete[name]" type="text" class="name" id="delete_name" value="sjlb_delete"><input name="delete[table_name]" type="hidden" class="table_name" value="{$_REQUEST[table_name]}" /></td>
                  <td><textarea cols="57" rows="3" name="delete[remark]" id="delete[remark]">此操作只会把数据状态修改为status=0，不会实现在数据库中删除，相当于回收站或者禁用的功能</textarea></td>
                </tr>
                <tr>
                  <td><input type="checkbox" checked="checked" value="foreverdelete" name="create_data[]" class="create_data_foreverdelete create_data" onclick="create_menu('.create_data_foreverdelete','foreverdelete')">
                  永久删除</td>
                  <td>
                    <select name="foreverdelete[tpl]" id="foreverdelete_tpl" onchange="set_menu('foreverdelete')">
                      <option url="javascript:diaolog_foreverdelete();" target="_self" position="main" value="">无需模板</option>
                  </select>
                    <input type="hidden" value="javascript:diaolog_foreverdelete();" id="foreverdelete_menu_url" name="foreverdelete[menu_url]" />
                    <input type="hidden" value="_self" id="foreverdelete_menu_target" name="foreverdelete[menu_target]" />
                  <input type="hidden" value="main" id="foreverdelete_menu_position" name="foreverdelete[menu_position]" />
                  </td>
                  <td><input name="foreverdelete[title]" type="text" class="title" value="删除" id="foreverdelete[title]">
                  <input name="foreverdelete[menu]" type="checkbox" class="action_menu foreverdelete_menu" value="1" checked="checked" style="display:none"/>
                  <input name="delete[menu_status]" type="checkbox" value="1" />
                    显示菜单</td>
                  <td><input name="foreverdelete[name]" type="text" class="name" id="foreverdelete_name" value="sjlb_foreverdelete"><input name="foreverdelete[table_name]" type="hidden" class="table_name" value="{$_REQUEST[table_name]}" /></td>
                  <td><textarea cols="57" rows="3" name="foreverdelete[remark]" id="foreverdelete[remark]">彻底在数据库中删除，不可以找回来</textarea></td>
                </tr>
                <tr>
                  <td><input type="checkbox" value="show" name="create_data[]" class="create_data_show create_data" onclick="create_menu('.create_data_show','show')">详细信息</td>
                  <td>
                    <select name="show[tpl]" id="show_tpl" onchange="set_menu('show')">
                      <option url="￥url" target="_self" position="main" value="show">默认详细信息模板</option>
                      <option url="￥url" target="_self" position="main" value="show_2">详细信息模板2</option>
                  </select>
                    <input type="hidden" value="￥url" id="show_menu_url" name="show[menu_url]" />
                    <input type="hidden" value="_self" id="show_menu_target" name="show[menu_target]" />
                  <input type="hidden" value="main" id="show_menu_position" name="show[menu_position]" /></td>
                  <td><input name="show[title]" type="text" class="title" value="详细信息" id="show[title]">
                  <input name="show[menu]" type="checkbox" class="action_menu show_menu" value="1" style="display:none"/>
                    <input name="show[menu_status]" type="checkbox" value="1" />
                    显示菜单</td>
                  <td><input name="show[name]" type="text" class="name" id="show_name" value="sjlb_show"><input name="show[table_name]" type="hidden" class="table_name" value="{$_REQUEST[table_name]}" /></td>
                  <td><textarea cols="57" rows="3" name="show[remark]" id="show[remark]">选定的单个数据的详细信息</textarea></td>
                </tr>
                <tr>
                  <td><input type="checkbox" value="export" name="create_data[]" class="create_data_export create_data" onclick="create_menu('.create_data_export','export')">导出数据</td>
                  <td>
                    <select name="export[tpl]" id="export_tpl" onchange="set_menu('export');">
                      <option url="￥url" target="_self" position="main" value="export">默认导出数据模板</option>
                  </select>
                    <input type="hidden" value="￥url" id="export_menu_url" name="export[menu_url]" />
                    <input type="hidden" value="_self" id="export_menu_target" name="export[menu_target]" />
                  <input type="hidden" value="main" id="export_menu_position" name="export[menu_position]" /></td>
                  <td><input name="export[title]" type="text" class="title" value="导出数据" id="export[title]">
                  <input name="export[menu]" type="checkbox" class="action_menu export_menu" value="1" style="display:none"/>
                     <input name="export[menu_status]" type="checkbox" value="1" />
                    显示菜单</td>
                  <td><input name="export[name]" type="text" class="name" id="export_name" value="sjlb_export"><input name="export[table_name]" type="hidden" class="table_name" value="{$_REQUEST[table_name]}" /></td>
                  <td><textarea cols="57" rows="3" name="export[remark]" id="export[remark]">可以根据index中筛选或者选择的数据项进行导出的数据，此操作要依赖于index的操作</textarea></td>
                </tr>
                 <tr>
                  <td><input type="checkbox" value="import" name="create_data[]" class="create_data_import create_data" onclick="create_menu('.create_data_import','import')">
                  导入数据</td>
                  <td>
                    <select name="import[tpl]" id="import_tpl" onchange="set_menu('import');">
                      <option url="￥url" target="_self" position="main" value="import">默认导入数据模板</option>
                  </select>
                    <input type="hidden" value="￥url" id="import_menu_url" name="import[menu_url]" />
                    <input type="hidden" value="_self" id="import_menu_target" name="import[menu_target]" />
                  <input type="hidden" value="main" id="import_menu_position" name="import[menu_position]" /></td>
                  <td><input name="import[title]" type="text" class="title" value="导入数据" id="import[title]">
                  <input name="import[menu]" type="checkbox" class="action_menu import_menu" value="1" style="display:none"/>
                     <input name="import[menu_status]" type="checkbox" value="1" />
                   显示菜单</td>
                  <td><input name="import[name]" type="text" class="name" id="import_name" value="sjlb_import"><input name="import[table_name]" type="hidden" class="table_name" value="{$_REQUEST[table_name]}" /></td>
                  <td><textarea cols="57" rows="3" name="import[remark]" id="import[remark]">对数据表进行批量导入</textarea></td>
                </tr>
              </table>
              </form>
              </td>
      </tr>
    <tr>
	  <th align="left"><strong>权限</strong></th></tr>
	<tr>
	  <td>
      <form id="form_role" name="form_role" method="post" action="{:U('Sys/Action/add_access')}">
	  <input name="app_id" type="hidden" class="app_id" value="" />
	  <input name="module_id" type="hidden" class="module_id" value="" />
      <input name="action_ids" type="hidden" class="action_ids" value="" />
      <foreach name="list_role" item="role">
	    <input name="role_id[{$role[id]}]" class="role_id" type="checkbox" value="{$role[id]}" <if condition="$role[id] eq 1 || $role[id] eq 2">checked="checked"</if> />{$role[name]}
      </foreach>
      </form></td>
    </tr>
</table>
<div><input name="all_form_button" type="button" id="all_form_button" onclick="check_all_form();" value="确认添加" /></div>
<script language="javascript">
function create_menu(dom,action){
	$("."+action+"_menu").attr('checked',$(dom).is(':checked'));
}
function check_all_form(){
	//检查是否选择了应用
	var select_app=0;
	$(".app_info").each(function() {
        if($(this).is(":checked")){
			select_app=1;
		}
    });
	if(select_app!=1){
		alert('请选择应用信息');
		return false;
	}
	//检查模块的
	if(!$('.module_id').val()){
		alert('请选择模块');
		return false;
	}
	//检查对应的操作是否已存在
	var create_data_length=$(".create_data:checked").length;
	var i=0,error=false;
	var module_id=$(".module_id").val();
	issubmit=true;//不知道为什么会提交多次，这个控制他只提交一次
	$(".create_data:checked").each(function() {
		var check_action=$('#'+$(this).val()+'_name').val();
		i++; //alert(i+'=='+create_data_length);
		$.ajax({
			dataType:'json',
			url:"{:U('check_action')}",
			data:"module_id="+module_id+"&action_name="+check_action,
			success:function (rs){
				if(rs.status==0){
					error=true;
				}else if(!error&&i==create_data_length&&issubmit==true){ //没有重复的action，并且是检查了最后一个时去提交表单
					//把权限赋予到菜单,这里因为AJAX返回延时的问题可能会出错。
					$(".menu_role_ids").val('');
					$("#form_role .role_id").each(function() {
						if($(this).is(':checked')){
							$(".menu_role_ids").val($(".menu_role_ids").val()+'['+$(this).val()+']');
						}
					});
					//提交应用
					submit_action();
					issubmit=false;
				}
			}
		});
    });
}

//提交操作
function submit_action(){
	$("#form_action").ajaxSubmit({
		dataType:'json',
		success:function(data){
			if(data.ids){
				$("#form_role .action_ids").val(data.ids);
				submit_role();
			}
		}
	});
}
//提交权限
function submit_role(){
	$("#form_role").ajaxSubmit({
		dataType:'json',
		success:function(data){
			if(data.status){
				alert('操作添加成功！');
				window.location.href=data.url;
			}else{
				alert('操作添加失败，可能已存在相同的操作！');	
			}
		}
	});
}
function set_menu(dom){
	var obj=$("#"+dom+"_tpl option:selected");
	var target=obj.attr('target');
	var url=obj.attr('url');
	var position=obj.attr('position');alert(position);
	$("#"+dom+"_menu_target").val(target);
	$("#"+dom+"_menu_url").val(url);
	$("#"+dom+"_menu_position").val(position);
}
</script>
</div>
<include file="./Tpl/Public/footer.html" />